import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-14  08:51
 */
public class TestJDBC1 {
    public static void main(String[] args) throws SQLException {
        //1.建立数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("@Tao126826.");

        //2.与客户端建立联系
        Connection connection = dataSource.getConnection();

        //3.建立sql语句
        String sql = "insert into student values (6,'刘涛')";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.将sql发送到服务器
        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        //5.释放内存
        statement.close();
        connection.close();
    }
}
